# Set C++20 standard (must be set before adding targets)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

file(GLOB_RECURSE SRCS
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c*
)

file(GLOB_RECURSE HEADERS
    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h*
)

# Target executable names.
set(FFMPEG_WRAPPER_TARGET "ffmpeg_wrapper")

# Add glog library support
set(GLOG_LIB_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/../logger/lib/glogd.lib")
set(GLOG_LIB_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/../logger/lib/glog.lib")
ADD_LOGICAL_TARGET("glog_lib" "${GLOG_LIB_DEBUG}" "${GLOG_LIB_RELEASE}")

include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${FFMPEG_INCLUDE_DIR} ${GLOG_INCLUDE_DIR})
link_directories(${FFMPEG_LIB_DIR})

set(FFMPEG_LIBS
    avcodec
    avformat
    avutil
    swscale
    swresample
)


# Set target output directory
SET_CEF_TARGET_OUT_DIR()

add_library(${FFMPEG_WRAPPER_TARGET} STATIC ${SRCS} ${HEADERS})
SET_LIBRARY_TARGET_PROPERTIES(${FFMPEG_WRAPPER_TARGET})
add_dependencies(${FFMPEG_WRAPPER_TARGET} logger)

target_compile_options(${FFMPEG_WRAPPER_TARGET} PRIVATE /std:c++20)

target_compile_definitions(${FFMPEG_WRAPPER_TARGET} PRIVATE __STDC_CONSTANT_MACROS)

set_target_properties(${FFMPEG_WRAPPER_TARGET} PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON)

target_link_libraries(${FFMPEG_WRAPPER_TARGET} ${FFMPEG_LIBS} glog_lib)

# Add test program
option(BUILD_TEST "Build test program" ON)
if(BUILD_TEST)
    set(TEST_TARGET "ffmpeg_wrapper_test")
    set(TEST_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/test/test_main.cc")
    
    add_executable(${TEST_TARGET} ${TEST_SRCS})
    SET_EXECUTABLE_TARGET_PROPERTIES(${TEST_TARGET})
    target_compile_options(${TEST_TARGET} PRIVATE /std:c++20)
    target_include_directories(${TEST_TARGET} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${FFMPEG_INCLUDE_DIR} ${GLOG_INCLUDE_DIR})
    target_link_libraries(${TEST_TARGET} ${FFMPEG_WRAPPER_TARGET} ${FFMPEG_LIBS})
endif()


# Define FFmpeg binary files based on the actual DLL names in the FFmpeg bin directory
set(FFMPEG_BINARY_FILES
    avcodec.dll
    avformat.dll
    avutil.dll
    swscale.dll
    swresample.dll
    avdevice.dll
    avfilter.dll
    postproc.dll
)

# If test is enabled, copy FFmpeg DLLs to the target output directory
if(BUILD_TEST)
    # Copy FFmpeg DLLs to the test executable output directory
    COPY_FILES("${TEST_TARGET}" "${FFMPEG_BINARY_FILES}" "${FFMPEG_ROOT}/bin/x64" "${CEF_TARGET_OUT_DIR}")
endif()
